from connector.DATAbaseDriver import DatabaseDriver

# 注册用户，需要用户名、用户密码、用户角色
def register_user(driver: DatabaseDriver, username: str, password: str, role: int):
    result: tuple = driver.select_one(
        'select user_id from user where user_name = "{}"'.format(username), log=True)
    if result is None:
        update: int = driver.insert(
            'insert into user(user_name, user_password, user_role) values ("{}","{}",'
            '{})'.format(username, password, role), log=True)
    else:
        return 0
    return update


def login(driver: DatabaseDriver, username: str, password: str) -> tuple:
    result: tuple = driver.select_one(
        'select user_id, user_role from user where user_name = "{}" and '
        'user_password = "{}"'.format(username, password), log=True)
    return result


def get_users(driver: DatabaseDriver) -> tuple:
    result: tuple = driver.select_all("select * from user")
    return result


def update_user(driver: DatabaseDriver, food_id: int, food_name: str, food_price: int, food_type: str):
    result: int = driver.update(
        'update user set food_name="{}", food_price="{}",food_type={} where '
        'food_id={}'.format(food_name, food_price, food_type, food_id))
    return result


# 重置密码
def rest_password(driver: DatabaseDriver, user_id: int, new_password:str):
    # 更新用户
    result: int = driver.update(
        'update user set user_password="{}" where user_id="{}"'.format(new_password, user_id))
    return result


# 根据用户id获取用户
def get_user_by_id(driver: DatabaseDriver, user_id: int) -> tuple:
    result: tuple = driver.select_one("select * from user where user_id={}".format(user_id))
    return result



# 根据用户名获取用户
def get_user_by_username(driver: DatabaseDriver, username):
    return driver.select_one(sql='select * from user where user_name="{}"'.format(username))


def register_user(driver: DatabaseDriver, username, password, user_role):
    return driver.insert(
        sql='insert into user(user_name, user_password,user_role) values ("{}","{}","{}")'.format(username, password,
                                                                                                  user_role))


def get_user_by_username_or_role(driver: DatabaseDriver, username: str, user_role: int) -> list:
    if user_role != None and username != '':
        return driver.select_all('select * from user where user_name = "{}" and '
                                 'user_role = {}'.format(username, user_role))
    else:
        if user_role is None:
            return driver.select_all('select * from user where user_name like "%{}%"'.format(username))
        else:
            return driver.select_all('select * from user where user_name like "%{}%" and user_role='
                                     '{}'.format(username, user_role))


def get_username_by_user_id(driver: DatabaseDriver, user_id: int):
    return driver.select_one('select user_name from user where user_id="{}"'.format(user_id))


# 根据用户名密码获取用户，登录的时候需要
def get_user_by_username_and_password(driver: DatabaseDriver, username, password):
    return driver.select_one(sql='select * from user where user_name="{}" and user_password="{}"'.format(username, password))


def delete_user(driver: DatabaseDriver, user_id: str):
    return driver.delete(sql='delete from user where user_id="{}"'.format(user_id))

def add_user(driver: DatabaseDriver, user_id: str,username: str, password: str, role: str):
    return driver.insert(sql='insert into user (user_id,user_name, user_password, user_role) '
                                'values ("{}","{}","{}","{}")'.format(user_id,username, password, role), log=True)


if __name__ == "__main__":
    driver = DatabaseDriver()
    print(get_user_by_id)
    driver.close()
